package com.spzx.order.receiver;


import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.order.service.IOrderInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class OrderReceiver {

    @Autowired
    private IOrderInfoService orderInfoService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PAYMENT_PAY, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY, durable = "true"),
            key = MqConst.ROUTING_PAYMENT_PAY
    ))

    @SneakyThrows
    public void Receiver(String orderNo, Message message, Channel channel) {
        //都可以
        log.info("监听订单状态修改执行" + orderNo);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        orderInfoService.proccessPayStatus(orderNo);
        try {
            orderInfoService.proccessPayStatus(orderNo);
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
            channel.basicNack(deliveryTag, false, false);
        }
    }
}
